<template>
  <div class="app-container order-class">
    <el-row style="margin-bottom:10px">
      <el-col :span="3">
        <el-input v-model="search.keyword" placeholder="Keyword" style="width:95%" />
      </el-col>
      <el-button type="primary" icon="el-icon-search" @click="gettableinto" />
      <el-button type="primary" @click="handleAddRole">Add</el-button>
    </el-row>
    <el-table
      :data="tableList"
      style="width: 100%;margin-top:30px;"
      :empty-text="empty_text"
      border
    >
      <el-table-column align="center" prop="id" label="ID" width="50">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column align="left" label="Condominium name">
        <template slot-scope="scope">{{ scope.row.title }}</template>
      </el-table-column>
      <el-table-column align="left" label="Remark">
        <template slot-scope="scope">{{ scope.row.remark }}</template>
      </el-table-column>
      <el-table-column align="header-center" label="List Order" width="100">
        <template slot-scope="scope">{{ scope.row.list_order }}</template>
      </el-table-column>
      <el-table-column align="center" label="Action" width="100">
        <template slot-scope="scope">
          <el-row>
            <el-col :span="12">
              <el-link class="el-icon-edit icon-class" @click="handleEdit(scope)" />
            </el-col>
            <el-col :span="12">
              <el-link class="el-icon-delete icon-class" @click="handleDelete(scope)" />
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? 'Edit Condominium' : 'New Condominium'"
    >
      <div style="margin:0 30px">
        <el-form ref="connectForm" :model="formData" :rules="rules" label-width="160px" label-position="left">
          <el-form-item label="Condominium Name" prop="title">
            <el-input v-model="formData.title" placeholder="Condominium name" />
          </el-form-item>
          <el-form-item label="List Order" prop>
            <el-input v-model="formData.list_order" type="number" placeholder="List Order" />
          </el-form-item>
          <el-form-item label="Remark" prop>
            <el-input v-model="formData.remark" type="textarea" :autosize="{ minRows: 2, maxRows: 2 }" placeholder="Remark" />
          </el-form-item>
        </el-form>
        <div style="text-align:right;">
          <el-button type="danger" @click="dialogVisible = false">Cancel</el-button>
          <el-button type="primary" @click="confirm">Confirm</el-button>
        </div>
      </div>
    </el-dialog>
    <el-pagination
      style="margin-top:10px;float:right"
      background
      layout="slot,prev,pager,next"
      :total="totalpage"
      :current-page="search.page"
      :page-size="search.per_page"
      @current-change="handleIndexChange"
    >
      <span class="el-pagination__total">Total {{ totalpage }}</span>
    </el-pagination>
  </div>
</template>

<script>
// import MyTest from './noets.vue'
import {
  getList,
  getDetails,
  save,
  del
} from '@/api/condominium/index'

const defaultInfo = {
  id: '',
  title: '',
  remark: '',
  list_order: 10000
}

export default {
  components: {
    // MyTest
  },
  data() {
    return {
      statuslist: [],
      formData: {
        id: '',
        title: '',
        remark: '',
        list_order: 10000
      },
      search: {
        keyword: '',
        page: 1,
        per_page: 10
      },
      empty_text: '',
      totalpage: 0,
      tableList: [],
      dialogVisible: false,
      dialogType: 'new',
      rules: {
        title: { required: true, message: 'Please enter a Condominium name', trigger: 'blur' }
      }
    }
  },
  watch: {
    dialogVisible(newValue, oldValue) {
      if (this.$refs['connectForm']) this.$refs['connectForm'].resetFields()

      if (!newValue) {
        this.formData = Object.assign({}, defaultInfo)
      }
    }
  },
  created() {
    // Mock: get all routes and roles list from server
    this.getTableData()
  },
  methods: {
    handleIndexChange(index) {
      this.search.page = index
      this.getTableData()
    },
    gettableinto() {
      this.keyword = this.keywords
      this.getTableData()
    },
    async getTableData() {
      this.empty_text = 'Loading ......'
      await getList(this.search).then(response => {
        this.tableList = response.data
        this.totalpage = response.total
        this.per_page = response.per_page
        this.current_page = response.current_page
        this.empty_text = 'Can\'t find record in table'
      }).catch(() => {
        this.tableList = []
        this.empty_text = 'Can\'t find record in table'
      })
    },
    handleAddRole() {
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      getDetails({ id: scope.row.id }).then(res => {
        this.formData = res
        this.dialogVisible = true
      })
    },
    handleDelete({ $index, row }) {
      this.$confirm('Confirm to remove the Condominium?', 'Warning', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      })
        .then(async() => {
          await del({ id: row.id })
          // baseAPI.get('user/witemplate/delete?id=' + row.id + '')
          this.tableList.splice($index, 1)
          this.getTableData()
          this.$message({
            type: 'success',
            message: 'Delete succed!'
          })
        })
        .catch(err => {
          console.error(err)
        })
    },
    async confirm() {
      this.$refs.connectForm.validate(async valid => {
        if (valid) {
          const data = Object.assign({}, this.formData)
          await save(data).then(async() => {
            this.dialogVisible = false
            await this.getTableData()
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
  .icon-class {
    font-size: 20px;
  }
}
</style>

<style>
.table-info-row1 td {
      color: #03be1cef;
 }
.table-info-row2 td {
      color: #0057facb;
 }
.table-info-row3 td {
      color: #fd0000c5;
 }
.order-class .el-table .cell {
  white-space: pre-line !important;
}
</style>
